package com.sergenis.miniapp.jpanel_imagenfondo;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;

public class AppFrame extends JFrame {
	
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private ImagePanel			contentPane;
	private Image				bgImage;
	private Image				logoImage;
	private JComboBox<ImageScale> comboBox;
	
	/**
	 * Create the frame.
	 */
	public AppFrame() {
		setTitle("SerGenis JPanel con fondo");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(800, 600);
		try {
			java.net.URL url = Main.class.getResource("/wallpaper.jpg").toURI()
					.toURL();
			this.bgImage = Toolkit.getDefaultToolkit().getImage(url);
			url = Main.class.getResource("/SerGenis.png").toURI().toURL();
			this.logoImage = Toolkit.getDefaultToolkit().getImage(url);
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
		contentPane = new ImagePanel(bgImage);
		contentPane.setBackground(java.awt.Color.BLACK);
		setContentPane(contentPane);
		initComponents();
	}
	
	private void initComponents() {
		// Init Fields
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0,
				Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);
		// Init Fields
		ImagePanel infoPanel = new ImagePanel();
		infoPanel.setPreferredSize(new Dimension(290, 300));
		infoPanel.setOpaque(true);
		infoPanel.setBackground(Color.LIGHT_GRAY);
		infoPanel.setImageScale(ImageScale.MOSAIC);
		GridBagConstraints gbc_infoPanel = new GridBagConstraints();
		gbc_infoPanel.anchor = GridBagConstraints.NORTHWEST;
		gbc_infoPanel.insets = new Insets(0, 0, 5, 5);
		gbc_infoPanel.gridx = 0;
		gbc_infoPanel.gridy = 0;
		contentPane.add(infoPanel, gbc_infoPanel);
		GridBagLayout gbl_infoPanel = new GridBagLayout();
		gbl_infoPanel.columnWidths = new int[] { 0, 0, 0, 0 };
		gbl_infoPanel.rowHeights = new int[] { 0, 0, 0 };
		gbl_infoPanel.columnWeights = new double[] { 1.0, 1.0, 1.0, Double.MIN_VALUE };
		gbl_infoPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		infoPanel.setLayout(gbl_infoPanel);
		
		JLabel lblTiposDeAjuste = new JLabel("Tipos de ajuste:");
		GridBagConstraints gbc_lblTiposDeAjuste = new GridBagConstraints();
		gbc_lblTiposDeAjuste.anchor = GridBagConstraints.EAST;
		gbc_lblTiposDeAjuste.fill = GridBagConstraints.VERTICAL;
		gbc_lblTiposDeAjuste.insets = new Insets(0, 0, 5, 5);
		gbc_lblTiposDeAjuste.gridx = 1;
		gbc_lblTiposDeAjuste.gridy = 0;
		infoPanel.add(lblTiposDeAjuste, gbc_lblTiposDeAjuste);
		
		comboBox = new JComboBox<ImageScale>();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changeImageScale();
			}
		});
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.gridx = 2;
		gbc_comboBox.gridy = 0;
		infoPanel.add(comboBox, gbc_comboBox);
		comboBox.setModel(new DefaultComboBoxModel(ImageScale.values()));
		comboBox.setSelectedItem(contentPane.getImageScale());
		// Init Fields
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		infoPanel.add(scrollPane, gbc_scrollPane);
		// Init Fields
		JTextPane txtpnenEstaApliacin = new JTextPane();
		txtpnenEstaApliacin.setContentType("text/html");
		txtpnenEstaApliacin.setOpaque(false);
		txtpnenEstaApliacin.setEditable(false);
		txtpnenEstaApliacin.setText("<html> \r\n<div>En esta apliación se muestra la apilación del componente ImagePanel y ImageBox para colocar una imagen de fondo y mostrar una imagen respectivamente. Las opciones que se pueden utilizar para mostrar la imagenes son:</div>\r\n<div><ul>\r\n<li><b>CENTRE: </b> <em>(Predeterminado)</em> Centra la imagenen en el componente sin cambier su tamaño.</li>\r\n\r\n<li><b>FILL:</b> La imagen es escalada para ocupar el alto del componente <em>(heigth)</em> y es centra horizontalmente.</li>\r\n\r\n<li><b>SET:</b> La imagen es escalada para ocupar el ancho del componente <em>(width)</em> y centrada verticalmente.</li>\r\n\r\n<li><b>EXPAND:</b> La imagen se redimeciona para ocupar en su totalidad al componente sin respetar la escala.</li>\r\n\r\n<li><b>MOSAIC:</b> La imagen no es escalada y se coloca alineandole con la esquina superior izquierda.</li>\r\n\r\n</ul></div></html>");
		scrollPane.setViewportView(txtpnenEstaApliacin);
		// Init Fields
		Component horizontalGlue = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
		gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalGlue.gridx = 1;
		gbc_horizontalGlue.gridy = 0;
		contentPane.add(horizontalGlue, gbc_horizontalGlue);
		
		ImageBox logoBox = new ImageBox();
		logoBox.setImageScale(ImageScale.CENTRE);
		logoBox.setPreferredSize(new java.awt.Dimension(310, 70));
		logoBox.setImage(logoImage);
		GridBagConstraints gbc_logoBox = new GridBagConstraints();
		gbc_logoBox.insets = new Insets(0, 0, 5, 5);
		gbc_logoBox.anchor = GridBagConstraints.NORTHEAST;
		gbc_logoBox.gridx = 2;
		gbc_logoBox.gridy = 0;
		contentPane.add(logoBox, gbc_logoBox);
	}
	private void changeImageScale(){
		ImageScale imagescale = (ImageScale) comboBox.getSelectedItem();
		contentPane.setImageScale(imagescale);
	}
	
}
